package org.ttitfly.zuoyue.service.room;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.ttitfly.zuoyue.dao.room.RoomProjectDao;
import org.ttitfly.zuoyue.entity.attachment.Attachment;
import org.ttitfly.zuoyue.entity.room.RoomProject;
import org.ttitfly.zuoyue.enums.RoomDataType;
import org.ttitfly.zuoyue.service.attachment.AttachmentManager;

@Component("roomProjectManager")
public class RoomProjectManager {
	
	
	@Resource
	private RoomProjectDao roomProjectDao;
	
	@Resource
	private AttachmentManager attachmentManager;
	
	
	public void save(RoomProject roomProject) {
		roomProjectDao.save(roomProject);
	}

	@Transactional
	public void update(RoomProject roomProject) {
		for (Attachment attachment : roomProject.getAttachments()) {
			if (attachment.getId() == null) {
				try {
					attachmentManager.upload(attachment);
				} catch (IOException e) {
					// 忽略一个文件不能上传
				}
			}
		}
		roomProjectDao.update(roomProject);
	}

	public void toRetainBase(RoomProject roomProject) {
		roomProject.setDataType(RoomDataType.BAK);
		update(roomProject);
	}
	
	public void toPuzzleBase(RoomProject roomProject) {
		roomProject.setDataType(RoomDataType.WHY);
		update(roomProject);
	}
	
	public RoomProject getByProcessInstanceId(int processInstanceId) {
		return roomProjectDao.getByProcessInstanceId(processInstanceId);
	}
	
	public RoomProject get(long id) {
		return roomProjectDao.get(id);
	}

	public Page<RoomProject> find() {
		return null;
	}
	
	/**
	 * 使用属性过滤条件查询覆盖延伸项目.
	 */
	@Transactional(readOnly = true)
	public Page<RoomProject> findAllRoomProjects(final Page<RoomProject> page, final List<PropertyFilter> filters) {
		return roomProjectDao.findPage(page, filters);
	}
	
	@Transactional
	public void removeAttachment(Long roomProjectId, Long attachmentId) {
		Attachment attachment = attachmentManager.get(attachmentId);
		if (!SpringSecurityUtils.getCurrentUserName().equals(attachment.getUsername())) {
			throw new RuntimeException("您没有权限删除该附件！");
		}
		roomProjectDao.deleteRelation(attachmentId);
		attachmentManager.remove(attachmentId);
	}
	
	@Transactional
	public boolean removeAttachment(Long attachmentId){
		boolean flag = roomProjectDao.deleteRelation(attachmentId);
		attachmentManager.remove(attachmentId);
		if(flag){
			return true;
		}else{
			return false;
		}
	}
	
	@Transactional
	public boolean removeProject(int processInstanceId){
		return roomProjectDao.removeProject(processInstanceId);
	}
	public int getRoomProject(String name){
		return roomProjectDao.getRoomProject(name);
	}

	/**
	 * 
	 * @param values	条件参数
	 * @return page
	 */
	public Page<RoomProject> findAllRoomProjects(String values,int pageNo,int pageSize){
		if (pageNo < 1) {
			pageNo = 1;
		}
		if (pageSize < 1) {
			pageSize = 20;
		}
		Page<RoomProject> page = new Page<RoomProject>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		List<RoomProject> list= new ArrayList<RoomProject>();
	    list=roomProjectDao.getAllRoomProject(values);
	    page.setTotalCount((long)list.size());
	    page.setResult(list);
		return page;
	}

	public RoomProject findRoomProject(int processInstanceId){
		return roomProjectDao.findRoomProject(processInstanceId);
	}
	
	public RoomProject findRoomProjectById(Long id){
		return roomProjectDao.findRoomProjectById(id);
	}
	
	public List<RoomProject> getRoomProjectByProIds(List<Integer> processInstanceIds){
		
		return roomProjectDao.getRoomProjectByProIds(processInstanceIds);
	}
}
